package com.morningstar.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.morningstar.common.dao.entity.CommonQuery;
import com.morningstar.common.result.Result;
import com.morningstar.common.result.ServiceCheckResult;
import com.morningstar.system.dao.entity.SysConfig;
import com.morningstar.system.service.SysConfigService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import static com.morningstar.common.constants.permission.PermissionConstants.*;

/**
 * 系统配置接口
 *
 * @author yanglianshan
 */
@Slf4j
@RestController
@RequestMapping("/system/config")
public class SysConfigController {

    private static final String PERMISSION = "system.config";
    private static final String PERMISSION_LIST = PERMISSION + LIST;
    private static final String PERMISSION_DETAIL = PERMISSION + DETAIL;
    private static final String PERMISSION_UPDATE = PERMISSION + UPDATE;
    private static final String PERMISSION_ADD = PERMISSION + ADD;
    private static final String PERMISSION_DELETE = PERMISSION + DELETE;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Resource
    private SysConfigService configService;

    /**
     * 系统配置列表
     *
     * @param commonQuery 通用查询类
     * @return IPage<SysConfig> 列表
     */
    @PostMapping(value = "/list")
    @SaCheckPermission(PERMISSION_LIST)
    public Result<IPage<SysConfig>> list(@RequestBody CommonQuery<SysConfig, SysConfig> commonQuery) {
        SysConfig query = commonQuery.getQuery();
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        if (query != null) {
            queryWrapper.like(StringUtils.isNotBlank(query.getConfigName()), SysConfig::getConfigName, query.getConfigName());
            queryWrapper.like(StringUtils.isNotBlank(query.getConfigKey()), SysConfig::getConfigKey, query.getConfigKey());
        }
        queryWrapper.orderByDesc(SysConfig::getCreateTime);
        return Result.successData(configService.page(commonQuery.getPage(), queryWrapper));
    }

    /**
     * 新增系统配置
     *
     * @param sysConfig 系统配置实体类
     * @return 基本结果类
     */
    @PostMapping
    @SaCheckPermission(PERMISSION_ADD)
    public Result<Object> add(@RequestBody SysConfig sysConfig) {
        ServiceCheckResult serviceCheckResult = configService.checkAddAndUpdate(sysConfig);
        if (serviceCheckResult.isResult()) {
            return Result.fail(serviceCheckResult.getMessage());
        }
        configService.save(sysConfig);
        redisTemplate.opsForValue().set(sysConfig.getConfigKey(), sysConfig.getConfigValue());
        return Result.successNoData();
    }

    /**
     * 修改系统配置
     *
     * @param sysConfig 系统配置实体
     * @return 基本结果类
     */
    @PostMapping("/update")
    @SaCheckPermission(PERMISSION_UPDATE)
    public Result<Object> update(@RequestBody SysConfig sysConfig) {
        ServiceCheckResult serviceCheckResult = configService.checkAddAndUpdate(sysConfig);
        if (serviceCheckResult.isResult()) {
            return Result.fail(serviceCheckResult.getMessage());
        }
        configService.updateById(sysConfig);
        redisTemplate.opsForValue().set(sysConfig.getConfigKey(), sysConfig.getConfigValue());
        return Result.successNoData();
    }

    /**
     * 删除系统配置
     *
     * @param configId  系统配置ID
     * @param configKey 系统配置KEY
     * @return 基本结果类
     */
    @GetMapping("/delete")
    @SaCheckPermission(PERMISSION_DELETE)
    public Result<Object> delete(@RequestParam("configId") Long configId, @RequestParam("configKey") String configKey) {
        //todo 系统内置不可删除
        configService.removeById(configId);
        redisTemplate.delete(configKey);
        return Result.successNoData();
    }

    /**
     * 详情
     *
     * @param configId 系统配置ID
     * @return 基本结果类
     */
    @GetMapping("/detail")
    @SaCheckPermission(PERMISSION_DETAIL)
    public Result<SysConfig> detail(@RequestParam("configId") Long configId) {
        return Result.successData(configService.getById(configId));
    }

}
